const { add, find, update, del,addAll } = require("../../server")
const department = require('../../model/department')

//查询全部数据 
const getCtr = async (req, res, next) => {
    try {
        let data = await find(department)
        res.send({
            code: 200,
            data,
        })
    } catch (err) {
        console.log(err);
        res.send({
            code: 400,
            message: '数据查询失败',
        })
    }
}

//新增 
const postCtr = async (req, res, next) => {
    let data = req.body
    try {
        await add(department, data)
        res.send({
            code: 200,
            message: '数据添加成功'
        })
    } catch (error) {
        res.send({
            code: 400,
            message: '数据添加失败'
        })
    }
}

// 文件导入
const postCtr1 = async (req, res, next) => {
    let data = req.body;
    try {
      await addAll(department, data);
      res.send({
        code: 200,
        message: "数据插入成功",
      });
    } catch (error) {
      console.log(error);
      res.send({
        code: 400,
        message: "数据插入失败",
      });
    }
  };

// 信息修改
const putCtr = async (req, res, next) => {
    let data = req.body
    try {
        let bob = await update(department, {
            people: data.people,
            nickname: data.nickname,
            destatues: data.destatues,
            departmentnum: data.departmentnum,
            createdTime: data.createdTime,
            departmentlist: data.departmentlist
        }, {
            id: data.id
        })
        if (bob == 0) {
            res.send({
                code: 400,
                message: '未进行修改操作'
            })
            return;
        }
        res.send({
            code: 200,
            message: '修改成功!'
        })
    } catch (error) {
        console.log(error);
        res.send({
            code: 400,
            message: '抱歉,修改失败!'
        })
    }
}

// 删除数据
const delCtr = async (req, res, next) => {
  try {
    let bob = await del(department, {
      id: req.body.id
    })
    if (bob == 0) {
      res.send({
        code: 400,
        message: '删除失败，请联系程序员'
      })
      return;
    }
    res.send({
      code: 200,
      message: '删除成功'
    })
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: '删除失败，请联系程序员'
    })
  }
}


const putCtr1 = async (req, res, next) => {
    let data = req.body
    console.log(data);
    try {
        let bob = await update(department, {
            people: data.people,
            departmentlist: data.departmentlist
        }, {
            id: data.id
        })
        if (bob == 0) {
            res.send({
                code: 400,
                message: '未进行修改操作'
            })
            return;
        }
        res.send({
            code: 200,
            message: '删除成功!'
        })
    } catch (error) {
        console.log(error);
        res.send({
            code: 400,
            message: '抱歉,修改失败!'
        })
    }
}

module.exports = {
    getCtr, postCtr, putCtr,putCtr1,delCtr,postCtr1,
}